{% extends "base.html" %}
{% load staticfiles %}

{% block title %}仿真策略{% endblock %}

{% block content %}
    {#上传策略模态框#}
    <div class="modal fade" data-backdrop="static" id="upmodal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">上传策略</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div style="margin-top:3%; display: block">
                            <label>策略名称</label>
                            <input class="opts" id="stgname" name="stgname" type="text"
                                   placeholder="策略名称">
                            <div id="stgname_upload_spaninfo" class="input-validation"></div>
                        </div>
                        <div style="margin-top:3%"><label>策略组</label>
                            <select class="opts" id="stggroup" name="stggroup">
                                <option name="isactive" value="1">
                                    做市策略
                                </option>
                                <option name="isactive" value="2">
                                    CTA策略
                                </option>
                                <option name="isactive" value="3">
                                    算法策略
                                </option>
                            </select>
                            <div id="stgroup_upload_spaninfo" class="input-validation"></div>
                        </div>
                        <div style="margin-top:3%">
                            <label>选择策略</label>
                            <input class="opts" id="file" name="file" type="file" accept=".py"
                                   onchange="filename1()">
                            <div id="stgfile_upload_spaninfo" class="input-validation"></div>
                        </div>
                        <div style="margin-top:3%"><label>策略参数</label>
                            <table style="margin-left: 40px">
                                <thead>
                                <tr class="activetable">
                                    <th>
                                        操作
                                    </th>
                                    <th>
                                        参数名*
                                    </th>
                                    <th>
                                        参数值*
                                    </th>
                                    <th>
                                        参数描述
                                    </th>
                                </tr>
                                </thead>
                                <tbody id="tb1">
                                <tr class="activetable">
                                    <td>
                                        <span style="padding:2px;cursor: pointer" class="btn-success"
                                              onclick="new_col('tb1')"><i class='fa fa-plus'></i>
                                        </span>
                                    </td>
                                    <td>
                                        <input class="activetable" name="stg_name" id="stg_name">
                                    </td>
                                    <td>
                                        <input class="activetable" name="stg_value" id="stg_value">
                                    </td>
                                    <td>
                                        <input class="activetable" name="stg_desc" id="stg_desc">
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <div id="paraarry_upload_spaninfo" class="input-validation"></div>
                        </div>
                        <hr/>
                        <div style="height: 20px; display: block">
                            <div style="padding-left:120px; float: left;">
                                <span class="btn-cancel btn-stc" onclick="fad_upmodeal_add_change()">取消</span>
                            </div>
                            <div style="padding-left:5px;float: left;">
                                <span class="btn-confirm btn-stc" onclick="reSub()">提交</span>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {# 上传策略提交时弹出确认按钮 #}
    <div class="modal fade" id="modal_resub">
    </div>
    {#打包失败模态框#}
    <div class="modal fade" data-backdrop="static" id="modal_packageError">
    </div>
    <!-- 申请实盘 -->
    <div class="modal fade" id="modal_askfor_live" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">申请实盘</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div style="margin-top:3%; display: block">
                            <label>策略ID</label>
                            <input class="opts conf" id="stgid_askfor_live" name="stgid" type="text"
                                   placeholder="策略ID" readonly>
                        </div>
                        <div style="margin-top:3%; display: block">
                            <label>策略名称</label>
                            <input class="opts conf" id="stgname_askfor_live" name="stgname" type="text"
                                   placeholder="策略名称" readonly>
                        </div>
                        <div style="margin-top:3%; display: block" id="stgroup">
                            <label>策略组</label>
                            <input class="opts conf" id="stggroup_askfor_live" name="stggroup" type="text"
                                   placeholder="策略组" readonly>
                        </div>
                        <div style="margin-top:3%; display: none" id="stgqb">
                            <label>QB账号</label>
                            <input class="opts" id="stgqb_askfor_live" name="stgqb" type="text" placeholder="QB账号">
                            <span id="stgqb_askFor_spaninfo" style="color: red;font-size: 12px"
                                  class="input-validation"></span>
                        </div>
                        <div style="margin-top:3%"><label>策略参数</label>
                            <table style="margin-left: 40px">
                                <thead>
                                <tr class="activetable">
                                    <th>
                                        参数名*
                                    </th>
                                    <th>
                                        参数值*
                                    </th>
                                    <th>
                                        参数描述
                                    </th>
                                </tr>
                                </thead>
                                <tbody id="tb2">
                                </tbody>
                            </table>
                            <span id="paraarry_askFor_spaninfo" style="color: red;font-size: 12px"
                                  class="input-stgpara"></span>
                        </div>
                        <hr/>
                        <div style="height: 20px; display: block">
                            <div style="padding-left:120px; float: left;">
                                <span class="btn-cancel btn-stc" onclick="fad_askfor_live()">取消</span>
                            </div>
                            <div style="padding-left:5px;float: left;">
                                <span class="btn-confirm btn-stc" onclick="reSubAskForLive()">提交</span>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- 申请实盘提交时弹出确认按钮 -->
    <div class="modal fade" id="modal_resubAskForLive"></div>
    <!-- 上传列表删除确认 -->
    <div class="modal fade" data-backdrop="static" id="modal_delete">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">删除仿真策略确认</h4>
                </div>
                <div class="modal-body">
                    <div style="margin-top:3%; display: block" id="confirm_paper">
                        确认删除该仿真策略？
                    </div>
                    <div style="height: 30px; display: block;margin-top: 20px">
                        <div style="padding-left:120px; float: left;">
                            <span class="btn-cancel btn-stc" onclick="fad_delete()">取消</span>
                        </div>
                        <div style="padding-left:5px; float: left;">
                            <span id="del" sid="" class="btn-confirm btn-stc" onclick="del_()">确定</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 仿真列表删除确认 -->
    <div class="modal fade" data-backdrop="static" id="modal_delete_paper">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">删除仿真策略确认</h4>
                </div>
                <div class="modal-body">
                    <div style="margin-top:3%; display: block" id="confirm_delete">
                        确认删除该仿真策略？
                    </div>
                    <div style="height: 30px; display: block;margin-top: 20px">
                        <div style="padding-left:120px; float: left;">
                            <span class="btn-cancel btn-stc" onclick="fad_delete_paper()">取消</span>
                        </div>
                        <div style="padding-left:5px; float: left;">
                            <span id="del_paper" sid_paper="" class="btn-confirm btn-stc"
                                  onclick="del_paper()">确定</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {#打包的模态框#}
    <div class="modal fade" data-backdrop="static" id="modal_pack">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">打包确认</h4>
                </div>
                <div class="modal-body">
                    <div style="margin-top:3%; display: block">
                        确认打包策略？
                    </div>
                    <div style="height: 30px; display: block;margin-top: 20px">
                        <div style="padding-left:120px; float: left;">
                            <span class="btn-cancel btn-stc" onclick="fad_pack()">取消</span>
                        </div>
                        <div style="padding-left:5px; float: left;">
                            <span id="attr_packid" packid="" class="btn-confirm btn-stc" onclick="stg_pack()">确定</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 上传成功模态框-->
    <div class="modal fade" data-backdrop="static" id="upload_success">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">上传策略</h4>
                </div>
                <div class="modal-body">
                    <div>
                        <p id="msg_upload" class="operat-success">
                            上传策略成功!</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 申请实盘成功 -->
    <div class="modal fade" data-backdrop="static" id="live_success">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">申请实盘</h4>
                </div>
                <div class="modal-body">
                    <div>
                        <p id="msg_live" class="operat-success">
                            申请实盘成功!</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {#打包成功模态框#}
    <div class="modal fade" data-backdrop="static" id="pack_success">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">打包</h4>
                </div>
                <div class="modal-body">
                    <div>
                        <p id="msg_pack" class="operat-success">
                            打包成功!</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {#删除成功模态框#}
    <div class="modal fade" data-backdrop="static" id="delete_success">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="delete-title">删除</h4>
                </div>
                <div class="modal-body">
                    <div>
                        <p id="msg_delete" class="operat-success">
                            策略删除成功!</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--业绩分析模态框-->
    <div class="modal fade" data-backdrop="static" id="modal_backtest_detail">
        <div class="modal-dialog" style="width: 1200px;height:600px">
            <div class="modal-content" style="background-color: #f5f5f5">
                <div class="modal-header" style="padding: 3px">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" style="padding: 5px 30px 0px 30px;height:auto;">
                    {# 指标信息#}
                    <div class="row L1">
                        <div class="col-md-12">
                            <div class="box box-primary">
                                <div class="box-header with-border">
                                    <h3 class="box-title">收益概览</h3>
                                    <div>
                                        <p></p>
                                        <table class="display table table-striped tablelist">
                                            <tbody>
                                            <tr style="background-color: #dddddd">
                                                <th data-align="center">起始日期</th>
                                                <th data-align="center">结束日期</th>
                                                <th data-align="center">回测收益</th>
                                                <th data-align="center">回测年化收益</th>
                                                <th data-align="center">夏普率</th>
                                                <th data-align="center">索提诺比率</th>
                                                <th data-align="center">calmar比率</th>
                                                <th data-align="center">年化波动率</th>
                                                <th data-align="center">最大回撤</th>
                                            </tr>
                                            <tr class="even">
                                                <td data-align="center" id="start_date"></td>
                                                <td data-align="center" id="end_date"></td>
                                                <td data-align="center" id="total_return"></td>
                                                <td data-align="center" id="annual_return"></td>
                                                <td data-align="center" id="sharpe_ratio"></td>
                                                <td data-align="center" id="sortino_ratio"></td>
                                                <td data-align="center" id="calmar_ratio"></td>
                                                <td data-align="center" id="annual_volatility"></td>
                                                <td data-align="center" id="max_draw_down"></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="box-header with-border">
                                    <h3 class="box-title">净值曲线</h3>
                                </div>
                                <div class="box-body " style="padding: 10px; height:350px;">
                                    <div id="cumulativereturns" style="height: 230px; width: 100%;">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Main content -->
    <body>
    <section>
        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active" style="margin-top:10px"><a href="#fa-upload" onclick="tabs(0)"
                                                              data-toggle="tab">上传列表</a></li>
                <li style="margin-top:10px"><a href="#fa-pack" onclick="tabs(1)" data-toggle="tab">打包列表</a></li>
                <li style="margin-top:10px"><a href="#fa-sim" onclick="tabs(2)" data-toggle="tab">仿真列表</a></li>
            </ul>
            <div class="tab-content">
                <!--上传列表-->
                <div class="tab-pane active" id="fa-upload">
                    <div class="div-query">
                        <div class="lbl-query">
                            <button class="btn-add" onclick="uploadmodal()">上传策略</button>
                        </div>
                        <div class="lbl-query"><span>策略ID </span>
                            <input class="input-query" type="text" id="ustgid">
                        </div>
                        <div class="lbl-query"><span>策略名称 </span>
                            <input class="input-query" type="text" id="ustgname">
                        </div>
                        <div class="lbl-query"><span>策略状态 </span>
                            <select class="input-query" id="ustgstatus">
                                <option value=""></option>
                                <option value="1">待审批</option>
                                <option value="2">审批驳回</option>
                                <option value="3">审批通过</option>
                            </select></div>
                        <div class="lbl-query">
                            <button class="btn-query" onclick="uploadsearch()">查询
                            </button>
                        </div>
                    </div>
                    <table class="table-stc" id="UploadTable"></table>
                </div>
                <!--打包列表-->
                <div class="tab-pane" id="fa-pack">
                    <div class="div-query">
                        <div class="lbl-query"><span>策略名称 </span>
                            <input class="input-query" type="text" id="pstgname">
                        </div>
                        <div class="lbl-query"><span>打包状态 </span>
                            <select class="input-query" id="ppackstatus">
                                <option value=""></option>
                                <option value="0">待打包</option>
                                <option value="1">打包成功</option>
                                <option value="2">打包失败</option>
                            </select></div>
                        <div class="lbl-query">
                            <button class="btn-query" onclick="packsearch()">查询
                            </button>
                        </div>
                    </div>
                    <table class="table-stc" id="packTable"></table>
                </div>
                <!--仿真列表-->
                <div class="tab-pane" id="fa-sim">
                    <div class="div-query">
                        <div class="lbl-query"><span>策略ID </span>
                            <input class="input-query" type="text" id="sstgid">
                        </div>
                        <div class="lbl-query"><span>策略名称 </span>
                            <input class="input-query" type="text" id="sstgname">
                        </div>
                        <div class="lbl-query"><span>运行状态 </span>
                            <select class="input-query" id="sstgstatus">
                                <option value=""></option>
                                <option value="1">启动</option>
                                <option value="4">停止</option>
                            </select></div>
                        <div class="lbl-query">
                            <button class="btn-query" onclick="papersearch()">查询
                            </button>
                        </div>
                    </div>
                    <table class="table-stc" id="simTable"></table>
                </div>
            </div>
        </div>
    </section>
    </body>
{% endblock %}

{% block scripts %}
    <script type="text/javascript">

        $(function () {
            uploadsearch()
        })

        //tab标签页
        function tabs(n) {
            if (n == 0) {
                uploadsearch();
                $('#fa-upload').addClass('active');
                $('#fa-pack').removeClass('active');
                $('#fa-sim').removeClass('active');
            } else if (n == 1) {
                packsearch();
                $('#fa-upload').removeClass('active');
                $('#fa-pack').addClass('active');
                $('#fa-sim').removeClass('active');
            } else {
                papersearch();
                $('#fa-upload').removeClass('active');
                $('#fa-pack').removeClass('active');
                $('#fa-sim').addClass('active');
            }
        }

        //上传策略取消按钮
        function fad_upmodeal_add_change() {
            $('#upmodal').modal('hide');
            $('#stgname').val('');
            $('#file').val('');
            $('#stg_name').val('');
            $('#stg_value').val('');
            $('#stg_desc').val('');
            $('#stgname_upload_spaninfo').text('');
            $('#stgfile_upload_spaninfo').text('');
            $('#paraarry_upload_spaninfo').text('');
            $('#tb1').html('')

            var tb1_html = '<tr class="activetable">\n' +
                '                                    <td>\n' +
                '                                        <span style="padding:2px;cursor: pointer" class="btn-success"\n' +
                '                                               onclick="new_col(\'tb1\')"><i class="fa fa-plus"></i>\n' +
                '                                        </span>\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_name" id="stg_name">\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_value" id="stg_value">\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_desc" id="stg_desc">\n' +
                '                                    </td>\n' +
                '                                </tr>'
            $('#tb1').append(tb1_html)
        }

        //上传策略模态框input框的值,
        function gets() {
            //仿真策略参数表的条数[{key,value,desc},{}]
            var pararry = []
            $("#tb1").find("tr").each(function () {
                var tdata = $(this).children()
                var paraname = tdata.eq(1).find("input").val()
                var paravalue = tdata.eq(2).find("input").val()
                var paradesc = tdata.eq(3).find("input").val()
                var paradic = {"paraname": paraname, "paravalue": paravalue, "paradesc": paradesc}
                pararry.push(paradic)
            })

            data = {
                'stgname': JSON.stringify([$('#stgname').val()]),
                'stggroup': JSON.stringify([$('#stggroup').val()]),
                // 'file': JSON.stringify([file]),
                'pararry': JSON.stringify(pararry),

            }
            return data;
        }

        //上传列表模态框显示
        function uploadmodal() {
            $('#tb1').html('')
            var tb1_html = '<tr class="activetable">\n' +
                '                                    <td>\n' +
                '                                        <span style="padding:2px;cursor: pointer" class="btn-success"\n' +
                '                                               onclick="new_col(\'tb1\')"><i class="fa fa-plus"></i>\n' +
                '                                        </span>\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_name" id="stg_name">\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_value" id="stg_value">\n' +
                '                                    </td>\n' +
                '                                    <td>\n' +
                '                                        <input class="activetable" name="stg_desc" id="stg_desc">\n' +
                '                                    </td>\n' +
                '                                </tr>'
            $('#tb1').append(tb1_html)
            $("input").val("");
            $("select").val("");
            $("#upmodal").modal("show")
        }

        //上传策略再次确认按钮
        function reSub() {
            //每次请求再次确认页面容器是空
            $('#modal_resub').html('');
            //上传策略点击提交，错误提示信息置空
            $('#stgname_upload_spaninfo').text('');
            $("#stgroup_upload_spaninfo").text('');
            $('#stgfile_upload_spaninfo').text('');
            $('#paraarry_upload_spaninfo').text('');
            //取页面输入的值
            var data1 = gets()
            var stgname = JSON.parse(data1["stgname"])[0];
            var stggroup_value = JSON.parse(data1["stggroup"])[0];
            var stggroup = $("#stggroup").find("option[value=" + stggroup_value + "]").text().replace(/\s+/g, "");
            var file = $("#file").val()
            var pararry = JSON.parse(data1["pararry"])
            var file_confirm = file.split("\\")[file.split("\\").length - 1]

            //输入框校验
            if ((stgname === '') || (stgname.length > 25)) {
                $('#stgname_upload_spaninfo').text('策略名不合法，请重新输入')
                return;
            } else if (stggroup === "") {
                $("#stgroup_upload_spaninfo").text("策略组不能为空")
                return;
            } else if (file === '') {
                $('#stgfile_upload_spaninfo').text('策略文件不可为空，请重新上传')
                return;
            }
            for (var i in pararry) {
                if (pararry[i].paraname === '') {
                    $('#paraarry_upload_spaninfo').text('参数名不能为空')
                    return;
                } else if (pararry[i].paravalue === '') {
                    $('#paraarry_upload_spaninfo').text('参数值不能为空')
                    return;
                } else if ((pararry[i].paraname).length > 25) {
                    $('#paraarry_upload_spaninfo').text('参数名不能超过25个字符')
                    return;
                } else if ((pararry[i].paravalue).length > 25) {
                    $('#paraarry_upload_spaninfo').text('参数值不能超过25个字符')
                    return;
                } else if ((pararry[i].paradesc).length > 25) {
                    $('#paraarry_upload_spaninfo').text('参数描述不能超过25个字符')
                    return;
                }
            }


            //向再次确认页面容器添加html
            var paradiv = ""
            for (let i = 0; i < pararry.length; i++) {
                var pararow = "<tr class=\"confirm_table\">\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_name\" value=" + pararry[i]["paraname"] + ">\n" +
                    "                                    </td>\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_value\" value=" + pararry[i]["paravalue"] + ">\n" +
                    "                                    </td>\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_desc\" value=" + pararry[i]["paradesc"] + ">\n" +
                    "                                    </td>\n" +
                    "                                </tr>";
                paradiv = paradiv + pararow
            }
            var confirm_strategy = '<div class="modal-dialog" style="width: 450px;">\n' +
                '            <div class="modal-content">\n' +
                '                <div class="modal-header">\n' +
                '                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>\n' +
                '                    <h4 class="modal-title">上传策略确认</h4>\n' +
                '                </div>\n' +
                '                <div class="modal-body" style="padding: 30px;height:auto">\n' +
                '                    <form>\n' +
                '                        <div style="margin-top:3%; display: block">\n' +
                '                            <label>策略名称</label>\n' +
                '                            <input disabled class="opts conf" name="stgname" type="text"\n' +
                '                                value="' + stgname + '"   >\n' +
                // '                                <span id="stgname_spaninfo" style="color: red; font-size: 14px"></span>\n' +
                '                        </div>\n' +
                '                        <div style="margin-top:3%"><label>策略组</label>\n' +
                '                            <input disabled class="opts conf" name="stggroup" type="text"\n' +
                '                                value="' + stggroup + '"   >\n' +
                // '                                <span id="stggroup_spaninfo" style="color: red; font-size: 14px"></span>\n' +
                '                        </div>\n' +
                '                        <div style="margin-top:3%"><label>选择文件</label>\n' +
                '                            <input disabled class="opts conf" name="file"' +
                '                                   value="' + file_confirm + '">\n' +
                // '                                <span id="file_spaninfo" style="color: red; font-size: 14px"></span>\n' +
                '                        </div>\n' +
                '                        <div style="margin-top:3%"><label>策略参数</label>\n' +
                '                            <table style="background-color: white; margin:20px">\n' +
                '                                <thead>\n' +
                '                                <tr class="confirm_table">\n' +
                '                                    <th>\n' +
                '                                        参数名*\n' +
                '                                    </th>\n' +
                '                                    <th>\n' +
                '                                        参数值*\n' +
                '                                    </th>\n' +
                '                                    <th>\n' +
                '                                        参数描述\n' +
                '                                    </th>\n' +
                '                                </tr>\n' +
                '                                </thead>\n' +
                '                                <tbody>\n' + paradiv +
                '                                </tbody>\n' +
                '                            </table>\n' +
                // '                                <span id="paraarry_spaninfo" style="color: red; font-size: 14px"></span>\n' +
                '                        </div>\n' +
                '                        <hr />\n' +
                '                        <div style="height: 20px; display: block">\n' +
                '                        <div style="padding-left:120px; float: left;">\n' +
                '                            <span class="btn-cancel btn-stc" onclick="reSub_fad()">取消</span>\n' +
                '                        </div>\n' +
                '                        <div style="padding-left:5px;float: left;">\n' +
                '                                <span class="btn-confirm btn-stc" onclick="sub(gets)">提交</span>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </form>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>'
            $("#modal_resub").append(confirm_strategy)
            $('#modal_resub').modal('show')

        }

        //上传策略提交逻辑
        function sub(func) {
            //此处输入框校验可不做，之前写过，懒得删了，留着无碍
            data = func();
            $('#stgname_spaninfo').text('');
            $('#file_spaninfo').text('');
            $('#paraarry_spaninfo').text('');

            data['file'] = JSON.stringify([files])
            for (let key in data) {
                var item = JSON.parse(data[key])[0]
                if (Boolean(item) == false && key != 'id') {
                    var title = $("#" + key).prev().text()
                    $("#" + key + "_spaninfo").text(title + '不能为空')
                    return;
                } else if (key === 'stgname') {
                    if (item.length > 25) {
                        $('#stgname_spaninfo').text('策略名称超过最大长度，请修改')
                        return;
                    }
                }
            }
            temp_para = JSON.parse(data['pararry'])

            if ((temp_para[0].paraname === '') || (temp_para[0].paravalue === '')) {
                $('#paraarry_spaninfo').text('策略参数不能为空')
                return;
            } else if (((temp_para[0].paraname).length > 25) || ((temp_para[0].paravalue).length > 25) || ((temp_para[0].paradesc).length > 25)) {
                $('#paraarry_spaninfo').text('策略参数超过最大长度，请修改')
                return;
            }


            //模态框取消
            fad_upmodeal_add_change()
            reSub_fad()
            loadon()

            $.post("{% url 'trade:paper' %}", data, function (r) {
                loadoff()
                $("#delete-title").text("上传策略")
                if (r) {
                    $("#UploadTable").bootstrapTable("destroy").bootstrapTable({
                        height: 600,
                        columns: [
                            {
                                field: 'stgid',
                                title: '策略ID'
                            }, {
                                field: 'stgname',
                                title: '策略名称'
                            }, {
                                field: 'createtime',
                                title: '上传时间'
                            }, {
                                field: 'auditstatus',
                                title: '审批状态',
                            }, {
                                field: 'uoption',
                                title: '操作',

                                formatter: function (value, row, index) {
                                    var stgname = row.stgname
                                    if (row.auditstatus !== "审批通过" && row.isopr === "1") {
                                        return "<span onclick=\"_delete('" + value + "','" + stgname + "')\" type='button' class='btn btn-danger btn-xs btn-flat btn_operation'> <i class='fa fa-trash'></i> 删除</span> ";
                                    } else {
                                        return ''
                                    }

                                }
                            },
                        ],
                        data: r,
                        // height: 600,
                        //******前端分页设置****
                        pagination: true,
                        pageNumber: 1,
                        pageSize: 20,
                        pageList: [10, 20, 50, 100],
                        paginationHAlign: "right",
                        paginationDetailHAlign: "none"
                        //******前端分页设置****
                    });
                    $("#msg_delete").text("上传策略成功！")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                } else {
                    $("#msg_delete").text("缺少仿真环境，请新建后重试！")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                }
            });
        }

        //上传策略再次确认模态框的取消
        function reSub_fad() {
            $('#modal_resub').modal('hide');
            $("#modal_resub").innerHTML = "";
        }

        //上传列表删除模态框取消
        function fad_delete() {
            $('#modal_delete').modal('hide');
        }

        //上传列表删除逻辑
        function del_() {
            d = $('#del').attr('sid');//该值是从js动态渲染删除里面拿到的，先_delete，再del_
            fad_delete()
            loadon()
            $.post("{% url 'trade:paper' %}", {'id': d, 'delete': 1}, function (r) {
                loadoff()
                if (r) {
                    $("#UploadTable").bootstrapTable("destroy").bootstrapTable({
                        height: 600,
                        columns: [
                            {
                                field: 'stgid',
                                title: '策略ID'
                            }, {
                                field: 'stgname',
                                title: '策略名称'
                            }, {
                                field: 'createtime',
                                title: '上传时间'
                            }, {
                                field: 'auditstatus',
                                title: '审批状态',
                            }, {
                                field: 'uoption',
                                title: '操作',

                                formatter: function (value, row, index) {
                                    var stgname = row.stgname
                                    if (row.auditstatus !== "审批通过" && row.isopr === "1") {
                                        return "<span onclick=\"_delete('" + value + "','" + stgname + "')\" type='button' class='btn btn-danger btn-xs btn-flat btn_operation'> <i class='fa fa-trash'></i> 删除</span> ";
                                    } else {
                                        return ''
                                    }

                                }
                            },
                        ],
                        data: r,
                        // height: 600,
                        //******前端分页设置****
                        pagination: true,
                        pageNumber: 1,
                        pageSize: 20,
                        pageList: [10, 20, 50, 100],
                        paginationHAlign: "right",
                        paginationDetailHAlign: "none"
                        //******前端分页设置****
                    });
                    $("#delete-title").text("删除")
                    $("#msg_delete").text("策略删除成功！")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                } else {
                    $("#msg_upload").text("服务器错误！")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                }
            });//post请求的回调函数，空值show"默认合约不可修改"，y值则删除后跳转。
        };

        //申请实盘再次确认按钮
        function reSubAskForLive() {
            //每次请求再次确认页面容器是空
            $('#modal_resubAskForLive').html('');
            //申请实盘点击提交，错误提示信息置空
            $('#paraarry_askFor_spaninfo').text('');


            //取页面输入的值
            var data1 = gets_live()
            var stgid = JSON.parse(data1["stgid_askfor_live"])[0];
            var stgname = JSON.parse(data1["stgname_askfor_live"])[0];
            var stggroup = JSON.parse(data1["stggroup_askfor_live"])[0];
            var pararry = JSON.parse(data1["pararry_askfor_live"])
            var qbnum = JSON.parse(data['stgqb_askfor_live'])[0];
            if (stggroup !== "算法策略" && qbnum === "") {
                $("#stgqb_askFor_spaninfo").text("QB账号为必填项！")
                return;
            }

            //输入框校验
            for (var i in pararry) {
                if (pararry[i].paravalue === '') {
                    $('#paraarry_askFor_spaninfo').text('策略参数值不能为空!')
                    return;
                } else if ((pararry[i].paravalue).length > 25) {
                    $('#paraarry_askFor_spaninfo').text('策略参数值不合法，请重新输入')
                    return;
                } else if ((pararry[i].paradesc).length > 25) {
                    $('#paraarry_askFor_spaninfo').text('策略描述不合法，请重新输入')
                    return;
                }
            }

            //根据qb是否有值，判断是否在确认框展示
            var qbdiv = ''
            if (qbnum) {
                qbdiv = '<div style="margin-top:3%">\n' +
                    '                            <label>QB账号</label>\n' +
                    '                            <input class="opts conf" value="' +
                    qbnum + '" name="stgqb" type="text" placeholder="QB账号">\n' +
                    '                        </div>'
            }


            //向再次确认页面容器添加html
            var paradiv = ""
            for (let i = 0; i < pararry.length; i++) {
                var pararow = "<tr class=\"confirm_table\">\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_name\" value=" + pararry[i]["paraname"] + ">\n" +
                    "                                    </td>\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_value\" value=" + pararry[i]["paravalue"] + ">\n" +
                    "                                    </td>\n" +
                    "                                    <td>\n" +
                    "                                        <input class=\"confirm_table\" disabled name=\"stg_desc\" value=" + pararry[i]["paradesc"] + ">\n" +
                    "                                    </td>\n" +
                    "                                </tr>";
                paradiv = paradiv + pararow
            }
            var confirm_strategy_askfor = '<div class="modal-dialog" style="width: 450px;">\n' +
                '            <div class="modal-content">\n' +
                '                <div class="modal-header">\n' +
                // '                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>\n' +
                '                    <h4 class="modal-title">申请实盘确认</h4>\n' +
                '                </div>\n' +
                '                <div class="modal-body">\n' +
                '                    <form>\n' +
                '                        <div style="margin-top:3%; display: block">\n' +
                '                            <label>策略ID</label>\n' +
                '                            <input disabled class="opts conf" name="stgid" type="text"\n' +
                '                                value="' + stgid + '"   >\n' +
                // '                                <span id="stgname_spaninfo" style="color: red; font-size: 14px"></span>\n' +
                '                        </div>\n' +
                '                        <div style="margin-top:3%; display: block">\n' +
                '                            <label>策略名称</label>\n' +
                '                            <input disabled class="opts conf" name="stgname" type="text"\n' +
                '                                value="' + stgname + '"   >\n' +
                '                        </div>\n' +
                '                        <div style="margin-top:3%"><label>策略组</label>\n' +
                '                            <input disabled class="opts conf" name="stggroup" type="text"\n' +
                '                                value="' + stggroup + '"   >\n' +
                '                        </div>\n' + qbdiv +
                '                        <div style="margin-top:3%"><label>策略参数</label>\n' +
                '                            <table style="margin-left: 40px">\n' +
                '                                <thead>\n' +
                '                                <tr class="confirm_table">\n' +
                '                                    <th>\n' +
                '                                        参数名*\n' +
                '                                    </th>\n' +
                '                                    <th>\n' +
                '                                        参数值*\n' +
                '                                    </th>\n' +
                '                                    <th>\n' +
                '                                        参数描述\n' +
                '                                    </th>\n' +
                '                                </tr>\n' +
                '                                </thead>\n' +
                '                                <tbody>\n' + paradiv +
                '                                </tbody>\n' +
                '                            </table>\n' +
                '                        </div>\n' +
                '                       <hr />\n' +
                '                       <div style="height: 20px; display: block">\n' +
                '                           <div style="padding-left:120px; float: left;">\n' +
                '                               <span class="btn-cancel btn-stc" onclick="reSubAskForLive_fad()">取消</span>\n' +
                '                           </div>\n' +
                '                       <div style="padding-left:5px;float: left;">\n' +
                '                           <span class="btn-confirm btn-stc" onclick="sub_live(gets_live)">提交</span>\n' +
                '                       </div>\n' +
                '                    </form>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>'
            $("#modal_resubAskForLive").append(confirm_strategy_askfor)
            if (stggroup === "算法策略") {
                $('#modal_resubAskForLive').modal('show')
            } else {
                checkqb()
            }

        }

        //申请实盘再次确认模态框取消
        function reSubAskForLive_fad() {
            $('#modal_resubAskForLive').modal('hide')
            $("#modal_resubAskForLive").html('');

        }

        //申请实盘模态框显示
        {#function modal_askfor_live() {#}
        {#    console.log($("#stggroup_askfor_live").val())#}
        {#    $("#modal_askfor_live").modal("show")#}
        {#    console.log("123132")#}
        {# }#}

        //申请实盘取消按钮
        function fad_askfor_live() {
            $('#modal_askfor_live').modal('hide');
            $("#tb2").html('');
        }

        //申请实盘，get请求，携带值
        function askfor_live_modify(id) {
            //模态框初始化
            $("#stgqb_askFor_spaninfo").text("")
            $('#paraarry_askFor_spaninfo').text('');
            $("#stgqb").css("display", "none")
            $("#stgqb_askfor_live").val("")
            $.get("{% url 'trade:paper' %}", {'askForLive_stgid': id}, function (r) {
                if (r) {
                    var data = (r.rows)[0]
                    for (var key in data) {
                        if (data[key] instanceof Array) {
                            for (let i = 0; i < data[key].length; i++) {
                                var pararow = "<tr class=\"activetable\">\n" +
                                    "              <td>\n" +
                                    "                  <input class=\"activetable\" disabled style='background-color: #eeeeee' name=\"stg_name\" value=" + data[key][i]["paraname"] + ">\n" +
                                    "              </td>\n" +
                                    "              <td>\n" +
                                    "                  <input class=\"activetable\" name=\"stg_value\" value=" + data[key][i]["paravalue"] + ">\n" +
                                    "              </td>\n" +
                                    "              <td>\n" +
                                    "                  <input class=\"activetable\" name=\"stg_desc\" value=" + data[key][i]["paradesc"] + ">\n" +
                                    "              </td>\n" +
                                    "           </tr>";
                                $("#tb2").append(pararow)
                            }
                        } else {
                            if (key === "stggroup" && data[key] !== "算法策略") {
                                $("#stgqb").css("display", "block")
                            }
                            if ($('#' + key + '_askfor_live')) {
                                $('#' + key + '_askfor_live').val(data[key]);
                            }
                        }

                    }
                }
            });

        };

        //校验qb账号是否存在(不需要给定策略id)
        function checkqb() {
            var qbnum = $("#stgqb_askfor_live").val()
            $.ajax({
                type: "post",
                url: "/trade/check_qb/",
                data:
                    {
                        "qbnum": qbnum,
                    },
                success: function (data) {
                    $("#stgqb_askFor_spaninfo").text(data)
                    if (!data) {
                        $('#modal_resubAskForLive').modal('show')
                    }
                }
            })
        }


        //申请实盘gets_live，获取页面输入或携带的值
        function gets_live() {

            //QB账号
            var stgqbval = $("#stgqb_askfor_live").val();


            //参数条数[{key,value,desc},{}]
            var pararry = [];

            $("#tb2").find("tr").each(function () {
                var tdata = $(this).children()
                var paraname = tdata.eq(0).find("input").val()
                var paravalue = tdata.eq(1).find("input").val()
                var paradesc = tdata.eq(2).find("input").val()
                var paradic = {"paraname": paraname, "paravalue": paravalue, "paradesc": paradesc}
                pararry.push(paradic)
            })

            data = {
                'stgid_askfor_live': JSON.stringify([$('#stgid_askfor_live').val()]),
                'stgname_askfor_live': JSON.stringify([$('#stgname_askfor_live').val()]),
                'envname_askfor_live': JSON.stringify([$('#envname_askfor_live').val()]),
                'stggroup_askfor_live': JSON.stringify([$('#stggroup_askfor_live').val()]),
                "stgqb_askfor_live": JSON.stringify([stgqbval]),
                'pararry_askfor_live': JSON.stringify(pararry)
            }
            return data;
        }

        //申请实盘逻辑
        function sub_live(func) {
            var data = func();
            fad_askfor_live()
            reSubAskForLive_fad()
            loadon()
            $.post("{% url 'trade:paper' %}", {'data': data, 'askfor_live_sub': 1}, function (r) {
                loadoff()
                if (r !== 'f') {
                    //实盘申请成功后去掉按钮
                    //申请实盘成功，申请实盘模态框和确认模态框隐藏，值置为空
                    $("#live_success").modal("show");
                    $("#simTable").bootstrapTable("destroy").bootstrapTable({
                        height: 600,
                        columns: [
                            {
                                field: 'stgid',
                                title: '策略ID'
                            }, {
                                field: 'stgname',
                                title: '策略名称'
                            }, {
                                field: 'createtime',
                                title: '上传时间',
                            }, {
                                field: 'runstatus',
                                title: '运行状态'
                            }, {
                                title: '开始运行时间',
                                field: 'starttime'
                            }, {
                                title: '结束运行时间',
                                field: 'endtime'
                            }, {
                                field: 'soption',
                                title: '操作',
                                halign: "center",
                                align: "right",
                                width: 220,

                                formatter: function (value, row, index) {
                                    var stop = '';
                                    var stgname = row.stgname;
                                    var askfor_live = '';
                                    var analy = "";
                                    //业绩分析按钮
                                    if (row.stggroup !== "3") {
                                        analy = "<span  class='btn btn-analysis btn-xs btn-flat btn_operation'  onclick=\"analysis('" + value + "')\"> <i class='fa fa-pencil'></i>业绩分析</span> ";
                                    }
                                    if (row.runstatus == '停止' && row.isopr === "1") {
                                        stop = "<span onclick=\"_delete_paper('" + value + "','" + stgname + "')\" class='btn btn-danger btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_model_field'> <i class='fa fa-trash'></i> 删除</span> "
                                    }
                                    if ((row.aud_status_live === '2' && row.isopr === "1") || (row.aud_status_live === '-1.1' && row.isopr === "1")) {
                                        askfor_live = "<span id='askreply' onclick=\"askfor_live_modify('" + value + "')\" type='button' class='btn btn-blue  btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_askfor_live'> <i class='fa fa-upload' ></i> 申请实盘</span> "
                                    }
                                    return stop + askfor_live + analy
                                }
                            },
                        ],
                        data: r,
                        // height: 600,
                        {#width: 100,#}
                        //******前端分页设置****
                        pagination: true,
                        pageNumber: 1,
                        pageSize: 20,
                        pageList: [10, 20, 50, 100],
                        paginationHAlign: "right",
                        paginationDetailHAlign: "left"
                        //******前端分页设置****
                    })
                    $("#askreply").attr("style", "display:none")
                    setTimeout(function () {
                        $("#live_success").modal("hide")
                    }, 2000)
                } else {
                    $("#msg_live").text("申请实盘失败，缺少实盘环境主服务器！")
                    $("#live_success").modal("show");
                    setTimeout(function () {
                        $("#live_success").modal("hide")
                    }, 2000)
                }
            });
        }


        //仿真列表模态框取消
        function fad_delete_paper() {
            $('#modal_delete_paper').modal('hide');
        }

        //打包模态框取消modal_pack
        function fad_pack() {
            $('#modal_pack').modal('hide');
        }

        //显示上传列表删除的模态框
        function _delete(d, stgname) {
            $("#confirm_paper").text("确认删除 " + "'" + stgname + "'" + " 策略?")
            $('#modal_delete').modal('show');
            $('#del').attr('sid', d);
        }

        //显示仿真列表删除的模态框
        function _delete_paper(d, stgname) {
            $('#modal_delete_paper').modal('show');
            $("#confirm_delete").text("确认删除 " + "'" + stgname + "'" + " 策略?")
            $('#del_paper').attr('sid_paper', d);
        }


        //仿真列表删除逻辑
        function del_paper() {
            d = $('#del_paper').attr('sid_paper');
            fad_delete_paper()
            loadon()
            $.post("{% url 'trade:paper' %}", {'id': d, 'delete_paper_list': 1}, function (r) {
                loadoff()
                if (r) {
                    $("#simTable").bootstrapTable("destroy").bootstrapTable({
                        height: 600,
                        columns: [
                            {
                                field: 'stgid',
                                title: '策略ID'
                            }, {
                                field: 'stgname',
                                title: '策略名称'
                            }, {
                                field: 'createtime',
                                title: '上传时间',
                            }, {
                                field: 'runstatus',
                                title: '运行状态'
                            }, {
                                title: '开始运行时间',
                                field: 'starttime'
                            }, {
                                title: '结束运行时间',
                                field: 'endtime'
                            }, {
                                field: 'soption',
                                title: '操作',
                                halign: "center",
                                align: "right",
                                width: 220,

                                formatter: function (value, row, index) {
                                    var stop = '';
                                    var stgname = row.stgname
                                    var askfor_live = '';
                                    var analy = "";
                                    //业绩分析按钮
                                    if (row.stggroup !== "3") {
                                        analy = "<span  class='btn btn-analysis btn-xs btn-flat btn_operation'  onclick=\"analysis('" + value + "')\"> <i class='fa fa-pencil'></i>业绩分析</span> ";
                                    }
                                    if (row.runstatus == '停止' && row.isopr === "1") {
                                        stop = "<span onclick=\"_delete_paper('" + value + "','" + stgname + "')\" class='btn btn-danger btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_model_field'> <i class='fa fa-trash'></i> 删除</span> "
                                    }
                                    if ((row.aud_status_live === '2' && row.isopr === "1") || (row.aud_status_live === '-1.1' && row.isopr === "1")) {
                                        askfor_live = "<span id='askreply' onclick=\"askfor_live_modify('" + value + "')\" type='button' class='btn btn-blue  btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_askfor_live'> <i class='fa fa-upload' ></i> 申请实盘</span> "
                                    }
                                    return stop + askfor_live + analy
                                }
                            },
                        ],
                        data: r,
                        // height: 600,
                        {#width: 100,#}
                        //******前端分页设置****
                        pagination: true,
                        pageNumber: 1,
                        pageSize: 20,
                        pageList: [10, 20, 50, 100],
                        paginationHAlign: "right",
                        paginationDetailHAlign: "left"
                        //******前端分页设置****
                    })
                    $("#modal_delete_paper").modal("hide")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                } else {
                    $("#msg_delete").text("服务器错误！")
                    $("#delete_success").modal("show");
                    setTimeout(function () {
                        $("#delete_success").modal("hide");
                    }, 2000)
                }
            });//post请求的回调函数，空值show"默认合约不可修改"，y值则删除后跳转。
        };

        //打包失败逻辑
        function stg_pack_error(d) {
            $('#modal_packageError').html("")
            var packError = '<div class="modal-dialog">\n' +
                '            <div class="modal-content">\n' +
                '                <div class="modal-header">\n' +
                '                    <h4 class="modal-title">打包失败提示</h4>\n' +
                '                </div>\n' +
                '                <div class="modal-body">\n' +
                '                   <div id="packError_spaninfo" style="margin-top:3%; display: block">\n' +
                '                   </div>\n' +
                '                <div style="height: 30px; display: block;margin-top: 20px">\n' +
                '                   <div style="padding-left:120px; float: left;">\n' +
                '                       <span id="packError_spaninfo" class="btn-confirm btn-stc" onclick="packErroBtnSub()">确定</span>\n' +
                '                   </div>\n' +
                '                </div>  \n' +
                '           </div>' +
                '       </div>' +
                '</div>'
            loadon()
            $.post("{% url 'trade:paper' %}", {'id': d, 'stg_pack_error': 1}, function (r) {
                loadoff()
                if (r.packmsg.length == 0) {
                    $('#modal_packageError').append(packError)
                    $('#packError_spaninfo').text("未知错误");
                    $('#modal_packageError').modal('show')
                } else {
                    $('#modal_packageError').append(packError)
                    $('#packError_spaninfo').text(r.packmsg);
                    $('#modal_packageError').modal('show')
                }
            });
        };

        //打包失败确认按钮
        function packErroBtnSub() {
            $('#modal_packageError').modal('hide');
        }

        //打包模态框展示
        function stg_pack_show(d) {
            $('#modal_pack').modal('show');
            $('#attr_packid').attr('packid', d);
        }

        //打包逻辑
        function stg_pack() {
            d = $('#attr_packid').attr('packid');
            var stgname = $("#pstgname").val();
            var packstatus = $("#ppackstatus").val();
            fad_pack()
            loadon()
            $.post("{% url 'trade:paper' %}", {
                'id': d,
                'stg_pack': 1,
                "stgname": stgname,
                "ppstatus": packstatus
            }, function (r) {
                loadoff()
                if (r) {
                    $("#packTable").bootstrapTable("destroy").bootstrapTable({
                        height: 600,
                        columns: [
                            {
                                field: 'stgid',
                                title: '策略ID'
                            }, {
                                field: 'stgname',
                                title: '策略名称'
                            }, {
                                field: 'packstatus',
                                title: '打包状态',
                            }, {
                                field: 'packtime',
                                title: '最后打包时间'
                            }, {
                                field: 'backtype',
                                title: '主/备'

                            }, {
                                field: 'envname',
                                title: '运行环境'

                            }, {
                                field: 'poption',
                                title: '操作',
                                halign: "center",
                                align: "right",

                                formatter: function (value, row, index) {
                                    if (row.packstatus == "打包失败" && row.isopr === "1") {
                                        return "<span onclick=\"stg_pack_show('" + value + "')\" type='button' class='btn btn-success btn-xs btn-flat btn_operation'> <i class='fa fa-file-zip-o'></i> 打包</span> " +
                                            "&nbsp;<span id='pack_error_btn_disable' onclick=\"stg_pack_error('" + value + "')\" class='btn btn-danger btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_model_field'> <i class='fa fa-warning'></i> 失败原因</span> ";
                                    } else {
                                        return ''
                                    }

                                }
                            },
                        ],
                        data: r,
                        // height: 600,
                        {#width: 100,#}
                        //******前端分页设置****
                        pagination: true,
                        pageNumber: 1,
                        pageSize: 20,
                        pageList: [10, 20, 50, 100],
                        paginationHAlign: "right",
                        paginationDetailHAlign: "left"
                        //******前端分页设置****
                    })
                    $("#modal_pack").modal("hide")
                    $("#msg_pack").text("手工打包完成")
                    $("#pack_success").modal("show");
                    setTimeout(function () {
                        $("#pack_success").modal("hide");
                    }, 2000)
                } else {
                    $("#msg_pack").text("服务器错误！")
                    $("#pack_success").modal("show");
                    setTimeout(function () {
                        $("#pack_success").modal("hide");
                    }, 2000)
                }
            });
        };


        //上传列表的查询
        function uploadsearch() {
            var ustgid = $("#ustgid").val();
            var ustgname = $("#ustgname").val();
            var ustgstatus = $("#ustgstatus").val();
            // 整理发送后台的数据
            var data = {
                "stgid": ustgid,
                "stgname": ustgname,
                "auditstatus": ustgstatus
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:paper' %}", {"data": JsonData, 'upload_search': 1}, function (r) {
                $("#UploadTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            field: 'stgid',
                            title: '策略ID',
                        }, {
                            field: 'stgname',
                            title: '策略名称'
                        }, {
                            field: 'createtime',
                            title: '上传时间'
                        }, {
                            field: 'auditstatus',
                            title: '审批状态',
                        }, {
                            field: 'uoption',
                            title: '操作',

                            formatter: function (value, row, index) {
                                var stgname = row.stgname
                                if (row.auditstatus !== "审批通过" && row.isopr === "1") {
                                    return "<span onclick=\"_delete('" + value + "','" + stgname + "')\" type='button' class='btn btn-danger btn-xs btn-flat btn_operation'> <i class='fa fa-trash'></i> 删除</span> ";
                                } else {
                                    return ''
                                }

                            }
                        },
                    ],
                    data: r,
                    // height: 600,
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "none"
                    //******前端分页设置****
                });
            });

        }

        //打包列表的查询
        function packsearch() {
            var pstgname = $("#pstgname").val();
            var ppackstatus = $("#ppackstatus").val();
            // 整理发送后台的数据
            var data = {
                "pstgname": pstgname,
                "ppackstatus": ppackstatus
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:paper' %}", {"data": JsonData, 'pack_search': 1}, function (r) {
                $("#packTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            field: 'stgid',
                            title: '策略ID'
                        }, {
                            field: 'stgname',
                            title: '策略名称'
                        }, {
                            field: 'packstatus',
                            title: '打包状态',
                        }, {
                            field: 'packtime',
                            title: '最后打包时间'
                        }, {
                            field: 'backtype',
                            title: '主/备'

                        }, {
                            field: 'envname',
                            title: '运行环境'

                        }, {
                            field: 'poption',
                            title: '操作',
                            halign: "center",
                            align: "right",

                            formatter: function (value, row, index) {
                                if (row.packstatus == "打包失败" && row.isopr === "1") {
                                    return "<span onclick=\"stg_pack_show('" + value + "')\" type='button' class='btn btn-success btn-xs btn-flat btn_operation'> <i class='fa fa-file-zip-o'></i> 打包</span> " +
                                        "&nbsp;<span id='pack_error_btn_disable' onclick=\"stg_pack_error('" + value + "')\" class='btn btn-danger btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_model_field'> <i class='fa fa-warning'></i> 失败原因</span> ";
                                } else {
                                    return ''
                                }

                            }
                        },
                    ],
                    data: r,
                    // height: 600,
                    {#width: 100,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });
        }

        //仿真列表的查询
        function papersearch() {
            var sstgid = $("#sstgid").val();
            var sstgname = $("#sstgname").val();
            var sstgstatus = $("#sstgstatus").val();
            // 整理发送后台的数据
            var data = {
                "sstgid": sstgid,
                "sstgname": sstgname,
                "sstgstatus": sstgstatus
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:paper' %}", {"data": JsonData, 'paper_search': 1}, function (r) {
                $("#simTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            field: 'stgid',
                            title: '策略ID'
                        }, {
                            field: 'stgname',
                            title: '策略名称'
                        }, {
                            field: 'createtime',
                            title: '上传时间',
                        }, {
                            field: 'runstatus',
                            title: '运行状态'
                        }, {
                            title: '开始运行时间',
                            field: 'starttime'
                        }, {
                            title: '结束运行时间',
                            field: 'endtime'
                        }, {
                            field: 'soption',
                            title: '操作',
                            halign: "center",
                            align: "right",
                            width: 220,

                            formatter: function (value, row, index) {
                                var stop = '';
                                var stgname = row.stgname
                                var askfor_live = '';
                                var analy = "";
                                //业绩分析按钮
                                if (row.stggroup !== "3") {
                                    analy = "<span  class='btn btn-analysis btn-xs btn-flat btn_operation'  onclick=\"analysis('" + value + "')\"> <i class='fa fa-pencil'></i>业绩分析</span> ";
                                }
                                if (row.runstatus === '停止' && row.isopr === "1") {
                                    stop = "<span onclick=\"_delete_paper('" + value + "','" + stgname + "')\" class='btn btn-danger btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_model_field'> <i class='fa fa-trash'></i> 删除</span> "
                                }
                                if ((row.aud_status_live === '2' && row.isopr === "1") || (row.aud_status_live === '-1.1' && row.isopr === "1")) {
                                    askfor_live = "<span id='askreply' onclick=\"askfor_live_modify('" + value + "')\" type='button' class='btn btn-blue btn-xs btn-flat btn_operation' data-toggle='modal' data-target='#modal_askfor_live'> <i class='fa fa-upload' ></i> 申请实盘</span> "
                                }
                                return stop + askfor_live + analy
                            }
                        },
                    ],
                    data: r,
                    // height: 600,
                    {#width: 100,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });

        }


        function new_col(idn) {
            var h = "<tr class='activetable'>" + '<td>' +
                '                                        <span style="padding:3px; cursor: pointer" class="btn-danger small"\n' +
                '                                              onclick="$(this).parent().parent().remove()"> <i class="fa fa-minus"></i>' +
                '                                        </span>' +
                '                                    </td>' +
                "<td><input class='activetable' name='stg_name'></td>" +
                "</td>" +
                "<td><input class='activetable' name='stg_value'></td>" +
                "<td><input class='activetable' name='stg_desc'></td>" +
                "</tr>";
            $('#' + idn).append($(h));
        };

        //全局变量，用于取得上传的文件
        var files = ""

        //只能上传py文件
        function filename1() {
            var filename_ = $("#file").val()
            {#var file = $("#file").files[0]#}
            if (filename_ && filename_.slice(-3) != ".py") {
                $('#stgfile_upload_spaninfo').text('请上传py为后缀的文件');
                $("#file").val("")
            }
            //文件上传
            const file = document.getElementById("file").files[0];
            if (file && file.size === 0) {
                $('#stgfile_upload_spaninfo').text('策略文件不可为空，请重新上传');
            }

            if (file) {
                {#console.log("234")#}
                var reader = new FileReader();
                {#reader.readAsBinaryString(file);#} //乱码
                {#reader.readAsDataURL(file); //需要base64解码，然后再解码（会有16进制的数据）#}
                reader.readAsText(file, "utf-8"); //大小不一致
                reader.onload = function () {
                    var file_ = reader.result
                    files = file_
                    {#console.log("files", file_)#}
                    if (file_) {
                        return file_
                    }
                }
            }
        }

        //业绩分析逻辑
        function analysis(stgid) {

            //收益概览获取值
            var td_ele = $("tr.even").children("td")
            td_ele.each(function () {
                $(this).text("-")
            })

            //遍历td，将后台获取的值根据元素的id进行渲染。
            $.get(" {% url 'trade:paper_incomeStatistics' %}", {'stgid': stgid}, function (r) {
                if (r) {
                    for (const key in r) {
                        if ($('#' + key)) {
                            $('#' + key).text(r[key]);
                        }

                    }
                }
            });

            //净值曲线获取值 + 图像渲染
            $.get("{%url 'trade:paper_cumulativereturns'%}", {'stgid': stgid}, function (result) {
                setTimeout(function () {
                    var myChart = echarts.init(document.getElementById('cumulativereturns'));
                    var option = {
                        tooltip: {
                            trigger: 'axis',
                            formatter: function (obj) {
                                return obj[0].seriesName + ":" + obj[0].value + '<br>';
                            }
                        },
                        grid: {
                            left: '5%',   // 与容器左侧的距离
                            right: '5%', // 与容器右侧的距离
                            top: '5%',   // 与容器顶部的距离
                            bottom: '22%', // 与容器底部的距离
                        },
                        dataZoom: [
                            {
                                show: true,
                                realtime: true,
                                start: 0,
                                end: 100
                            },
                            {
                                type: 'inside',
                                realtime: true,
                                start: 0,
                                end: 100
                            }
                        ],
                        xAxis: {
                            type: 'category',
                            data: null,
                            scale: true
                        },
                        yAxis: {
                            type: 'value',
                            scale: true
                        },
                        series: [{
                            name: '净值',
                            data: null,
                            type: 'line',
                        },
                        ]
                    };
                    option.series[0].data = result.retData;
                    option.xAxis.data = result.retDate;
                    myChart.setOption(option);
                }, 500)

            })


            $("#modal_backtest_detail").modal("show")


        }

    </script>
    <style>
        {#加载数据时的样式#}
        .bootstrap-table .fixed-table-container .fixed-table-body .fixed-table-loading {
            align-items: center;
            background: #141414;
            display: none;
            justify-content: center;
            position: absolute;
            bottom: 0;
            width: 100%;
            z-index: 1000;
        }
    </style>

{% endblock %}